package com.example.administrator.newfragmentation.activity.simple;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.newfragmentation.R;
import com.example.administrator.newfragmentation.fragment.simple.DiscoverFragment;
import com.example.administrator.newfragmentation.fragment.simple.LoginFragment;
import com.example.administrator.newfragmentation.fragment.simple.ShopFragment;
import com.example.administrator.newfragmentation.fragmentMain.BaseBlankFragment;
import com.example.administrator.newfragmentation.fragmentMain.BaseFragment;
import com.example.administrator.newfragmentation.fragmentMain.SimpleFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.SupportFragment;

public class SimpleActivity extends SupportActivity implements
        NavigationView.OnNavigationItemSelectedListener
        ,LoginFragment.OnLoginSuccessListener{

    @Bind(R.id.simple_container)
    FrameLayout simpleContainer;
    @Bind(R.id.nav_view)
    NavigationView navView;
    @Bind(R.id.drawer_layout)
    DrawerLayout drawerLayout;

    private TextView mTvName;   // NavigationView上的名字
    private ImageView mImgNav;  // NavigationView上的头像
    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        ButterKnife.bind(this);

        if(savedInstanceState == null){
            loadRootFragment(R.id.simple_container, SimpleFragment.newInstance());
        }

        initView();
    }

    private void initView() {
        navView.setNavigationItemSelectedListener(this);
        navView.setCheckedItem(R.id.nav_home);

        LinearLayout llNavHeader = (LinearLayout) navView.getHeaderView(0);
        mTvName = (TextView) llNavHeader.findViewById(R.id.tv_name);
        mImgNav = (ImageView) llNavHeader.findViewById(R.id.img_nav);
        llNavHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.closeDrawer(GravityCompat.START);

                drawerLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        goLogin();
                    }
                }, 250);
            }
        });
    }

    private void goLogin() {
        start(LoginFragment.newInstance());
    }

    @Override
    public void onBackPressedSupport() {
        if(drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START);
        }else {
            Fragment topFragment = getTopFragment();

            // 主页的Fragment
            if (topFragment instanceof BaseBlankFragment) {
                navView.setCheckedItem(R.id.nav_home);
            }

            if(getSupportFragmentManager().getBackStackEntryCount() > 1){
                // 出栈当前Fragment(在当前Fragment所在栈内pop)
                pop();

            }else{
                if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
                    finish();
                } else {
                    TOUCH_TIME = System.currentTimeMillis();
                    Toast.makeText(this, R.string.press_again_exit, Toast.LENGTH_SHORT).show();
                }
            }

        }
    }

    @Override
    public boolean onNavigationItemSelected(final MenuItem item) {
        drawerLayout.closeDrawer(GravityCompat.START);

        drawerLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                int id = item.getItemId();
                SupportFragment topFragment = getTopFragment();
                switch (id){
                    case R.id.nav_home:
                        SimpleFragment simpleFragment = findFragment(SimpleFragment.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("from", "主页-->来自:" + topFragment.getClass().getSimpleName());
                        simpleFragment.putNewBundle(bundle);
                        start(simpleFragment,SupportFragment.SINGLETASK);
                        break;

                    case R.id.nav_discover:
                        DiscoverFragment discoverFragment = findFragment(DiscoverFragment.class);
                        if(discoverFragment == null){
                            // 出栈某一个Fragment栈内之上的Fragments
                            // 如果想出栈后，紧接着.beginTransaction()开始一个新事务，请使用下面的方法：
                            // 防止多事务连续执行的异常
                            //false,代表SimpleFragment不消失,不出栈
                            popTo(SimpleFragment.class, false, new Runnable() {
                                @Override
                                public void run() {
                                    start(DiscoverFragment.newInstance());
                                }
                            });
                        }else{
                            start(DiscoverFragment.newInstance(),SupportFragment.SINGLETASK);
                        }
                        break;

                    case R.id.nav_msg:
                        ShopFragment shopFragment = findFragment(ShopFragment.class);
                        if(shopFragment == null){
                            popTo(SimpleFragment.class, false, new Runnable() {
                                @Override
                                public void run() {
                                    start(ShopFragment.newInstance());
                                }
                            });
                        }else{
                            start(ShopFragment.newInstance(),SupportFragment.SINGLETASK);
                        }
                        break;
                }
            }
        },250);
        return true;
    }

    @Override
    public void onLoginSuccess(String account) {
        mTvName.setText(account);
        mImgNav.setImageResource(R.drawable.ic_account_circle_white_48dp);
        Toast.makeText(this, "登录成功,NavigationView的用户名已经更改!", Toast.LENGTH_SHORT).show();
    }
}
